import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {AchieveDetail} from "../../../model/pages/performance/group-achieve";
import {PerformanceProvider} from "../../../providers/perfomance/performance/performance";
import {TimeUtil} from "../../../model/util/TimeUtil";
import {HttpErrorResponse} from "@angular/common/http";
import {PersonalGroupPerformancePage} from "../personal-group-performance/personal-performance";
import {PerformanceType} from "../../../model/enum/performance";
import {LoadingService} from "../../../services/common/loading.service";

/**
 * Generated class for the GroupPerformanceListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-group-performance-list',
    templateUrl: 'group-performance-list.html',
    providers: [PerformanceProvider, LoadingService]
})
export class GroupPerformanceListPage {
    /**
     * 筛选时间
     * @type {string}
     */
    filterDate = '';
    /**
     * 是否在加载数据
     * @type {boolean}
     */
    isLoaded: boolean = false;
    /**
     * 小组列表详情数据
     * @type {any[]}
     */
    groupPerformanceList: Array<AchieveDetail> = [];
    /**
     * 类型（Jurisdiction：所辖小组列表；Municipality:直辖小组列表）
     * @type {number}
     */
    type: number = PerformanceType.Jurisdiction;
    /**
     * 视图控制器
     */
    @ViewChild(Content) content: Content;
    /**
     * 获取数据订阅
     */
    getDataSubscription: any;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private performanceProvider: PerformanceProvider,
        private toastController: ToastController,
        private loadingService: LoadingService) {
    }

    ionViewDidLoad() {
        // console.log('ionViewDidLoad GroupPerformanceListPage');
        // 默认筛选时间为当前月份
        this.filterDate = TimeUtil.getLocalTime(Date.now());
        // 获取页面类型
        this.type = this.navParams.get('type');
        // 开始查询数据
        this.filter();
    }

    ionViewWillLeave() {
        // 取消订阅数据
        if (this.getDataSubscription) {
            this.getDataSubscription.unsubscribe();
        }
    }

    /**
     * 返回上一级
     */
    goBack() {
        this.navCtrl.pop();
    }


    /**
     * 根据时间筛选数据
     */
    filter() {
        // 显示loading动画
        this.loadingService.showLoading();
        // 正在加载数据
        this.isLoaded = false;
        // 请求数据
        this.getDataSubscription = this.performanceProvider.getGroupAchieveList(this.filterDate, this.type).subscribe(data => {

            this.groupPerformanceList = data;

            this.isLoaded = true;
            // 关闭loading动画
            this.loadingService.dismissLoading();
        }, (error: HttpErrorResponse) => {
            this.groupPerformanceList = [];
            this.isLoaded = true;
            // 关闭loading动画
            this.loadingService.dismissLoading();
        })
    }

    /**
     * 跳转至直辖组业绩详情
     * @param {AchieveDetail} data
     *
     */
    goGroupPerformancePage(data: AchieveDetail) {

        // 跳转至直辖组业绩详情
        this.navCtrl.push("PersonalGroupPerformancePage", {
            type: this.type,
            month: this.filterDate,
            data: data
        })


    }

    goUp() {
        this.content.scrollToTop();
    }


}
